package com.wxw.BaseLibrary.adapter;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author david
 * @date 2020-03-12
 */
public class ViewsHelper {
    private Map<String, View> viewMap = new HashMap<>();

    public ViewsHelper() {
        
    }

    public Map<String, View> getViewMap() {
        return viewMap;
    }

    public void setViewMap(Map<String, View> viewMap) {
        this.viewMap = viewMap;
    }

    public ImageView getImageView(String key) {
        View view = viewMap.get(key);
        if (view instanceof ImageView) {
            return (ImageView)view;
        }
        return null;
    }

    public void setImageResource(String key, int imgId) {
        ImageView imageView = getImageView(key);
        if (imageView != null) {
            imageView.setImageResource(imgId);
        }
    }

    public TextView getTextView(String key) {
        View view = getView(key);
        if (view != null && view instanceof TextView) {
            return (TextView)view;
        }
        return null;
    }

    public void setText(String key, String txt) {
        TextView textView = getTextView(key);
        if (textView != null) {
            textView.setText(txt);
        }
    }

    public EditText getEditText(String key) {
        View view = getView(key);
        if (view != null && view instanceof EditText) {
            return (EditText)view;
        }
        return null;
    }

    public void setEditText(String key, String value) {
        EditText editText = getEditText(key);
        if (editText != null) {
            editText.setText(value);
        }
    }

    public View getView(String key) {
        if (viewMap.containsKey(key)) {
            return viewMap.get(key);
        }
        return null;
    }
}